#include "StdAfx.h"
#include "Workspace.h"

#include "LittleRegistry.h"


//////////////////////////////////////////////////////////////////////////
// CBCGPWorkspace

RegeditWorkspace* g_pLittleWorkspace = nullptr;
BOOL           g_bLittleWorkspaceAutocreated = FALSE;

RegeditWorkspace* LittleGetWorkspace()
{ 
	//---------------------------------------------------------------------
	// You must either:
	// ----------------
	// a) construct a CBCGPWorkspace object
	// b) mix a CBCGPWorkspace class somewhere in (e.g. your CWinApp object)
	// c) call CBCGPWorkspace::UseWorkspaceManager() to automatically
	//    initialize an object for you
	//---------------------------------------------------------------------
	ASSERT (nullptr != g_pLittleWorkspace);
	return g_pLittleWorkspace; 
}



//-----------------------
// clean up if necessary:
//-----------------------
struct _LITTLE_WORKSPACE_TERM
{
	~_LITTLE_WORKSPACE_TERM(){
		if (nullptr != g_pLittleWorkspace && g_bLittleWorkspaceAutocreated){
			delete g_pLittleWorkspace;
			g_pLittleWorkspace = nullptr;
			g_bLittleWorkspaceAutocreated = FALSE;
		}
	}
};
static const _LITTLE_WORKSPACE_TERM workspaceTermLittle;






//////////////////////////////////////////////////////////////////////////
//  [6/6/2018 LittleRan]

RegeditWorkspace::RegeditWorkspace(BOOL bResourceSmartUpdate /* = FALSE */)
{
	TRACE(_T("CBCGPWorkspace Init \n"));
}



RegeditWorkspace::~RegeditWorkspace(void)
{
	TRACE(_T("CBCGPWorkspace Destroy \n"));
}



BOOL RegeditWorkspace::UseWorkspaceManager(LPCTSTR lpszSectionName /*=nullptr*/, BOOL bResourceSmartUpdate)
{
	if (nullptr != g_pLittleWorkspace){
		return FALSE;	// already exists
	}

	g_pLittleWorkspace = new RegeditWorkspace (bResourceSmartUpdate);
	g_bLittleWorkspaceAutocreated = TRUE;	// Cleanup

	if(nullptr != lpszSectionName){
		g_pLittleWorkspace->m_strRegSection = lpszSectionName;
	}

	return TRUE;
}



LPCTSTR RegeditWorkspace::SetRegistryBase(LPCTSTR lpszSectionName /*= nullptr*/)
{
	m_strRegSection = (nullptr != lpszSectionName ) ? lpszSectionName : _T("");

	return m_strRegSection;
}



LPCTSTR	RegeditWorkspace::GetRegistryBase()
{
	return m_strRegSection;
}



CString	RegeditWorkspace::GetRegSectionPath(LPCTSTR szSectionAdd /*=nullptr*/)
{
	CString strSectionPath = ::LittleGetRegPath (m_strRegSection);
	if (nullptr != szSectionAdd && 0 != _tcslen (szSectionAdd)){
		strSectionPath += szSectionAdd;
		strSectionPath += _T("\\");
	}

	return strSectionPath;
}



int RegeditWorkspace::GetInt(LPCTSTR lpszEntry, int nDefault /*= 0*/)
{
	return GetSectionInt(_T(""), lpszEntry, nDefault);
}



double RegeditWorkspace::GetDouble(LPCTSTR lpszEntry, double dblDefault /*= 0.0*/)
{
	return GetSectionDouble(_T(""), lpszEntry, dblDefault);
}



CString	RegeditWorkspace::GetString(LPCTSTR lpszEntry, LPCTSTR lpszDefault /*= ""*/)
{
	return GetSectionString(_T(""), lpszEntry, lpszDefault);
}



BOOL RegeditWorkspace::GetBinary(LPCTSTR lpszEntry, LPBYTE* ppData, UINT* pBytes)
{
	return GetSectionBinary(_T(""), lpszEntry, ppData, pBytes);
}



BOOL RegeditWorkspace::GetObject(LPCTSTR lpszEntry, CObject& obj)
{
	return GetSectionObject(_T(""), lpszEntry, obj);
}



BOOL RegeditWorkspace::WriteInt(LPCTSTR lpszEntry, int nValue )
{
	return WriteSectionInt(_T(""), lpszEntry, nValue);
}



BOOL RegeditWorkspace::WriteDouble(LPCTSTR lpszEntry, double dblValue )
{
	return WriteSectionDouble(_T(""), lpszEntry, dblValue);
}



BOOL RegeditWorkspace::WriteString(LPCTSTR lpszEntry, LPCTSTR lpszValue )
{
	return WriteSectionString(_T(""), lpszEntry, lpszValue);
}



BOOL RegeditWorkspace::WriteBinary(LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes)
{
	return WriteSectionBinary(_T(""), lpszEntry, pData, nBytes);
}



BOOL RegeditWorkspace::WriteObject(LPCTSTR lpszEntry, CObject& obj)
{
	return WriteSectionObject(_T(""), lpszEntry, obj);
}



int RegeditWorkspace::GetSectionInt( LPCTSTR lpszSubSection, LPCTSTR lpszEntry, int nDefault /*= 0*/)
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);

	int nRet = nDefault;

	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, TRUE);

	if (reg.Open (strSection)){
		reg.Read (lpszEntry, nRet);
	}
	return nRet;
}


double RegeditWorkspace::GetSectionDouble( LPCTSTR lpszSubSection, LPCTSTR lpszEntry, double dblDefault /*= 0.0*/)
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	
	double dblRet = dblDefault;

	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, TRUE);

	if (reg.Open (strSection)){
		reg.Read (lpszEntry, dblRet);
	}
	return dblRet;
}



CString RegeditWorkspace::GetSectionString( LPCTSTR lpszSubSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault /*= ""*/)
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	ASSERT(lpszDefault);
	
	CString strRet = lpszDefault;

	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, TRUE);

	if (reg.Open (strSection)){
		if (!reg.Read (lpszEntry, strRet)){
			strRet = lpszDefault;
		}
	}

	return strRet;
}



BOOL RegeditWorkspace::GetSectionBinary(LPCTSTR lpszSubSection, LPCTSTR lpszEntry, LPBYTE* ppData, UINT* pBytes)
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	ASSERT(ppData);
	
	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, TRUE);

	if (reg.Open (strSection) && reg.Read (lpszEntry, ppData, pBytes) ) {
		return TRUE;
	}
	return FALSE;
}



BOOL RegeditWorkspace::GetSectionObject(LPCTSTR lpszSubSection, LPCTSTR lpszEntry, CObject& obj)
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	ASSERT_VALID(&obj);
	
	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, TRUE);

	if (reg.Open (strSection) && reg.Read (lpszEntry, obj)) {
		return TRUE;
	}
	return FALSE;
}



BOOL RegeditWorkspace::WriteSectionInt( LPCTSTR lpszSubSection, LPCTSTR lpszEntry, int nValue )
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	
	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, FALSE);

	if (reg.CreateKey (strSection)){
		return reg.Write (lpszEntry, nValue);
	}
	return FALSE;
}



BOOL RegeditWorkspace::WriteSectionDouble( LPCTSTR lpszSubSection, LPCTSTR lpszEntry, double dblValue )
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	
	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, FALSE);

	if (reg.CreateKey (strSection))
	{
		return reg.Write (lpszEntry, dblValue);
	}
	return FALSE;
}



BOOL RegeditWorkspace::WriteSectionString( LPCTSTR lpszSubSection, LPCTSTR lpszEntry, LPCTSTR lpszValue )
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	ASSERT(lpszValue);

	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, FALSE);

	if (reg.CreateKey (strSection)){
		return reg.Write (lpszEntry, lpszValue);
	}
	return FALSE;
}



BOOL RegeditWorkspace::WriteSectionBinary(LPCTSTR lpszSubSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes)
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	ASSERT(pData);

	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, FALSE);

	if (reg.CreateKey (strSection)){
		return reg.Write (lpszEntry, pData, nBytes);
	}
	return FALSE;
}



BOOL RegeditWorkspace::WriteSectionObject(LPCTSTR lpszSubSection, LPCTSTR lpszEntry, CObject& obj)
{
	ASSERT(lpszSubSection);
	ASSERT(lpszEntry);
	ASSERT_VALID(&obj);

	CString strSection = GetRegSectionPath(lpszSubSection);

	LittleRegistrySP regSP;
	LittleRegistry& reg = regSP.Create (FALSE, FALSE);

	if (reg.CreateKey (strSection)){
		return reg.Write (lpszEntry, obj);
	}

	return FALSE;
}



